package cc;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DownloadUtils {

    //	下载视频
    public static void downloadVideo(String url, String fileName) throws IOException {
        System.out.println(new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date()) + "  DownloadUtils.downloadVideo=>> url:" + url + "  fileName:" + fileName);
        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        URLConnection connection = realUrl.openConnection();
        // 设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 建立实际的连接
        connection.connect();
        InputStream is = connection.getInputStream();
        byte[] bs = new byte[1024];
        int len;
        File dir = new File("D:/tmpFile");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String realName = fileName + ".mp4";
        OutputStream os = new FileOutputStream("D:/tmpFile/" + realName);
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        os.close();
        is.close();
    }
}
